package org.jupiter.blog.web.controller;

import org.jupiter.blog.api.domain.JupiterComment;
import org.jupiter.blog.api.dto.JupiterCommentDTO;
import org.jupiter.blog.service.service.JupiterCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
public class JupiterCommentController {

	@Autowired
	private JupiterCommentService commentService;

	/**
	 * 创建评论
	 */
	@PostMapping("/createComment")
	public ResponseEntity<JupiterComment> createComment(@RequestBody JupiterCommentDTO comment) {
		commentService.createComment(comment);
		return ResponseEntity.ok(null);
	}

	/**
	 * 获取文章的评论列表
	 */
	@GetMapping("/article/{articleId}")
	public ResponseEntity<List<JupiterCommentDTO>> getCommentsByArticleId(@PathVariable String articleId) {
		List<JupiterCommentDTO> comments = commentService.getCommentsByArticleId(articleId);
		return ResponseEntity.ok(comments);
	}

	/**
	 * 删除评论
	 */
	@DeleteMapping("/{uuid}")
	public ResponseEntity<Void> deleteComment(@PathVariable String uuid) {
		boolean deleted = commentService.deleteComment(uuid);
		return deleted ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
	}

	/**
	 * 点赞评论
	 */
	@PostMapping("/{uuid}/like")
	public ResponseEntity<Integer> likeComment(@PathVariable String uuid) {
		try{
			int likeCount = commentService.likeComment(uuid);
			return ResponseEntity.ok(likeCount);
		} catch (IllegalArgumentException e){
			return ResponseEntity.notFound().build();
		}
	}
}
